package org;

import org.entities.Item;
import org.entities.LevelObject;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Color;
import org.newdawn.slick.Input;
import org.newdawn.slick.Image;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Font;

public class StatusState  extends BasicGameState {

    private static int loopX;
    private static int loopY;
    private StateBasedGame game;
    private Image status,sndVol;//,manual;    
    private Font font;
    private boolean left,right,up,down;
    private static int x;
    private int y;
    private int currentPressTime = 0;    
    private int maxPressTime = 100;      
    private boolean nextPressOK;   
    public static boolean ctrlLoop;   
    public static final int ID = 9;
    public int getID() {
        return ID;
    }


    public void init(GameContainer container, StateBasedGame game) throws SlickException {

        this.game = game;		
        status = new Image("data/image/statusScreen.png");
        font = new AngelCodeFont("data/font/weasel.fnt","data/font/weasel.png");
        sndVol = new Image("data/image/sndVol.png");
        //manual = new Image("data/image/manual.png");
        x=64;
        y=63;
        //Progress.itemBlockAt[0][0] = new String("manual");
    }    

    public void update(GameContainer container, StateBasedGame game, int delta){

        if (currentPressTime < maxPressTime) {
            currentPressTime += delta;
            nextPressOK = false;
        }
        if (currentPressTime >= maxPressTime) {
            nextPressOK = true;
            currentPressTime =0;
        }
        if (nextPressOK){
            move();
        }        
    }

    private void move(){
        
        if (left) {
            if (!(x<=64))
                x-=32;
            System.out.printf("x/32=[%d], y/32=[%d]\n",(x/32)-2,(y/32)-1);
        }
        if (right) {
            if (!(x>=320))
                x+=32;
            System.out.printf("x/32=[%d], y/32=[%d]\n",(x/32)-2,(y/32)-1);
        }
        if (up) {
            if (!(y<=63))
                y-=32;
            System.out.printf("x/32=[%d], y/32=[%d]\n",(x/32)-2,(y/32)-1);
        }
        if (down) {
            if (!(y>=511))
                y+=32;
            System.out.printf("x/32=[%d], y/32=[%d]\n",(x/32)-2,(y/32)-1);
        }
    }

    public static void RegisterItem(String type){
        for (loopY=0;loopY<15;loopY++){ 
            for (loopX=0;loopX<9;loopX++){              
                    if (!ctrlLoop){
                    if (getBlockAt(loopX,loopY)==null){
                        Progress.storeItem.add(Progress.itemBlockAt[loopX][loopY] = new String(type)); 
                        Progress.gotItemInLevel[BlockMap.thisLevelNu]= new String("true");
                        System.out.printf("register item type=[%s]\n",type);
                        System.out.printf("inventory: stored item at Pos=[%d][%d]\n",loopX,loopY);
                        System.out.printf("no more item in level=[%d]\n",BlockMap.thisLevelNu);
                        //loopX=0;
                        //loopY=0;
                        //x=0;
                        ctrlLoop=true;
                        break;
                    }  
                }
            }           
        }
    }

    public static String getBlockAt(int x, int y) {
        return Progress.itemBlockAt[x][y];
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException{ 
        if (!LevelObject.debugMode)
            g.scale(2f, 2f);
        g.drawImage(status,0,0); 
        g.setFont(font);   
        //g.setColor(Color.white);
        //g.scale(0.6f, 0.6f);
        //g.drawString("Status", 313, -2);
        // =================
        // display item info
        // =================  
        g.resetTransform();
        if (getBlockAt((x/32)-2,(y/32)-1)!=null){
            g.setColor(Color.white);
            g.drawString(Item.infoSelect(getBlockAt((x/32)-2,(y/32)-1).toString()), 400, 325);
        }
        // =============
        // display items
        // =============
        for (loopX=0;loopX<9;loopX++){
            for (loopY=0;loopY<15;loopY++){               

                if (getBlockAt(loopX,loopY)!=null){//&&getBlockAt(loopX,loopY).equals("true")){
                    g.translate(64, 64);
                    g.drawImage(Item.imgSelect(getBlockAt(loopX,loopY).toString()), loopX*32, loopY*32);
                    //System.out.printf("image string=[%s]\n",getBlockAt(loopX,loopY).toString());
                    g.resetTransform();
                }  
            }
        }
        g.resetTransform();
        g.setColor(Color.green);
        g.drawRect(x, y, 32, 32);
        if (!LevelObject.debugMode)
           g.scale(2f, 2f);  
        // =======
        // shield
        // =======
        if (Progress.shields>30) g.setColor(Color.green); else g.setColor(Color.red);
        g.fillRect(313, 42, Progress.shields/2, 8);
        
        // =======
        // health
        // =======
        if (Progress.health>30) g.setColor(Color.green); else g.setColor(Color.red);
        g.fillRect(313, 68, Progress.health/2, 8);
        
        // =======
        // munition
        // =======
        if (Progress.energy>30) g.setColor(Color.green); else g.setColor(Color.red);
        g.fillRect(313, 94, Progress.energy/2, 8);
        
        // =========
        // energy
        // =========
        if (Progress.strength>30) g.setColor(Color.green); else g.setColor(Color.red);
        g.fillRect(313, 120, Progress.strength/2, 8);

        // ============
        // draw meters
        // ============
        g.setClip(312,41,416,215);
        //g.drawRect(317, 49, 50, 72);
        g.drawImage(sndVol, 313, 42);
        g.drawImage(sndVol, 313, 68);
        g.drawImage(sndVol, 313, 94);
        g.drawImage(sndVol, 313, 120);
        g.clearClip();   
    }

    
    public void keyPressed(int key, char c) {

        if (key == Input.KEY_LEFT) {
            left=true;
        }
        if (key == Input.KEY_RIGHT) {
            right=true;
        }
        if (key == Input.KEY_UP) {
            up=true;
        }
        if (key == Input.KEY_DOWN) {
            down=true;
        }        
    }


    public void keyReleased(int key, char c) {

        if (key == Input.KEY_TAB) {
            game.enterState(GameState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        }
        if (key == Input.KEY_LSHIFT) {
            game.enterState(MapState.ID, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
        }
        if (key == Input.KEY_LEFT) {
            left=false;
        }
        if (key == Input.KEY_RIGHT) {
            right=false;
        }
        if (key == Input.KEY_UP) {
            up=false;
        }
        if (key == Input.KEY_DOWN) {
            down=false;
        }
    }
}
